home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / libs / mthr25 / demo2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-29  |  2.3 KB  |  101 lines

  1. /* DEMO2.C  - MicroThread demonstration written by I H Ting
  2.  
  3.   This example demonstrates basic multi-threading. Three threads
  4.   are created initially. One waits for user input at the keyboard,
  5.   another update the status on the screen, and another to bounce
  6.   a 'ball' around the screen. Pressing 'a' will cause a new
  7.   'ball-bouncing' thread to be added each time until the maximum
  8.   number of threads is reached. Pressing 'q' stops the program.
  9. */
  10.  
  11. #include "mthread.h"
  12.  
  13. #define TRAIL_LEN 15
  14.  
  15. int numBalls=0;
  16.  
  17.  
  18. /* Ball bouncing routine */
  19. void BounceBall(void)
  20. {
  21.   char ball='o';
  22.   long x,y,ix,iy;
  23.   int xBall, yBall, xTrail[TRAIL_LEN], yTrail[TRAIL_LEN],
  24.       iBall, iTrail, iBlank;
  25.  
  26.   for(iTrail=0; iTrail<TRAIL_LEN; iTrail++){
  27.     xTrail[iTrail]=2;
  28.     yTrail[iTrail]=2;
  29.   }
  30.   x=y=200;
  31.   ix=iy=100;
  32.   iBall=TRAIL_LEN-1;
  33.   iTrail=TRAIL_LEN-2;
  34.   iBlank=0;
  35.   while(1){
  36.     xBall=(int)(x/100);
  37.     yBall=(int)(y/100);
  38.     if(xTrail[iTrail]!=xBall || yTrail[iTrail]!=yBall){
  39.       MTxyputc(xBall,yBall,ball);
  40.       MTxyputc(xTrail[iTrail],yTrail[iTrail],'.');
  41.       MTxyputc(xTrail[iBlank],yTrail[iBlank],' ');
  42.       xTrail[iBall]=xBall;
  43.       yTrail[iBall]=yBall;
  44.       iBall = (iBall+1) % TRAIL_LEN;
  45.       iBlank = (iBlank+1) % TRAIL_LEN;
  46.       iTrail = (iTrail+1) % TRAIL_LEN;
  47.     }
  48.     x += ix;
  49.     y += iy;
  50.     ix = x > 7900 ? -MTrandom(100) : ix;
  51.     ix = x < 200 ? MTrandom(100) : ix;
  52.     iy = y > 2300 ? -MTrandom(100) : iy;
  53.     iy = y < 300 ? MTrandom(100) : iy;
  54.   }
  55. }
  56.  
  57.  
  58. void InfoThread(void)
  59. {
  60.   MTxyputs(1,1, "MicroThread Demonstration. Press 'a' to add an ball, 'q' to quit");
  61.   while(1){
  62.     MTxyprintf(1,25,"Number of balls : %d", numBalls);
  63.   }
  64. }
  65.  
  66.  
  67.  
  68. /* Waits for the key 'q' to be pressed to end multitasking */
  69. void InputThread(void)
  70. {
  71.   int retval, priority;
  72.  
  73.   while(1){
  74.     if (MTgetch()=='a'){
  75.       priority = numBalls % 5 +1;
  76.       retval = MTAddThread(BounceBall,priority);
  77.       if(retval) numBalls++;
  78.     }
  79.     if (MTgetch()=='q'){
  80.       MTEndMultiThreading();
  81.     }
  82.   }
  83. }
  84.  
  85.  
  86.  
  87. /* the main program */
  88. void main(void)
  89. {
  90.   MTInitialise();
  91.   MTAddThread(InfoThread,1);
  92.   MTAddThread(InputThread,5);
  93.   MTAddThread(BounceBall,1);
  94.   numBalls++;
  95.   MTclrscr();
  96.   MTStartMultiThreading();
  97.   MTclrscr();
  98. }
  99.  
  100.  
  101.